package com.hotelManagerSystem.controller;


import com.github.pagehelper.PageInfo;
import com.hotelManagerSystem.model.Permission;
import com.hotelManagerSystem.service.IPermissionService;
import com.hotelManagerSystem.service.IRolePermissionService;
import com.hotelManagerSystem.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  权限端控制器
 * </p>
 *
 * @author longyajr
 * @since 2021-12-22
 */
@Controller
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    private IPermissionService iPermissionService;
    @Autowired
    private IRolePermissionService iRolePermissionService;

    /**
     * @author treasy
     * @description 获取所有权限信息并分页
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(Model model,
                       @RequestParam(required = false,defaultValue="1",value="pageNum")Integer pageNum,
                       @RequestParam(defaultValue="8",value="pageSize")Integer pageSize){
        PageUtil.runStartPage(pageNum,pageSize);
        List<Permission> permission_dbList = iPermissionService.queryPermissionList();
        PageInfo pageInfo = PageUtil.getPageInfo(permission_dbList);
        model.addAttribute("pageInfo",pageInfo);
        return "admin/permissionPage/permission_list";
    }

    //    权限添加页面跳转
    @RequestMapping("/addUI")
    public String addUI(Model model){
//        System.out.println("------------addUI---------------");
        //获取所有节点
        List<Permission> permissionList = iPermissionService.list();
        System.out.println(permissionList);
        model.addAttribute("permissionList",permissionList);
        return "admin/permissionPage/add-permission";
    }

    //    权限添加
    @RequestMapping("/add")
    @ResponseBody
    public Map<String, Object> add(Model model,Permission permission){
        Map<String, Object> map = new HashMap<>();
        boolean b = iPermissionService.save(permission);
        if (b){
            map.put("code",200);
            map.put("message","添加成功");
        }else {
            map.put("code",500);
            map.put("message","添加失败");
        }
        return map;
    }

    /**
     * @author treasy
     * @description 根据id删除权限
     * @param permissionId
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Map<String, Object> delete(int permissionId){
        Map<String, Object> map = new HashMap<>();
        boolean b = iPermissionService.removeById(permissionId);
        boolean b1 = iRolePermissionService.deleteByPermissionId(permissionId);
        if (b&&b1){
            map.put("code",200);
            map.put("message","删除成功");
        }else {
            map.put("code",500);
            map.put("message","删除失败");
        }
        return map;
    }

    //    权限编辑页面跳转

    /**
     * @author treasy
     * @param model
     * @param permissionId
     * @return
     */
    @RequestMapping("/updateUI")
    public String updateUI(Model model,int permissionId){
//        System.out.println("------------updateUI---------------");
        Permission permission = iPermissionService.getById(permissionId);
        model.addAttribute("permission",permission);
        List<Permission> permissionList = iPermissionService.list();
        model.addAttribute("permissionList",permissionList);
        return "admin/permissionPage/edit-permission";
    }

    /**
     * @author treasy
     * @param model
     * @param permission
     * @return
     */
    //    权限编辑
    @RequestMapping("/update")
    @ResponseBody
    public Map<String, Object> update(Model model,Permission permission){
        Map<String, Object> map = new HashMap<>();
        boolean b = iPermissionService.saveOrUpdate(permission);
        if (b){
            map.put("code",200);
            map.put("message","更新成功");
        }else {
            map.put("code",500);
            map.put("message","更新失败");
        }
        return map;
    }



}

